package com.szrt.day20161024;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

import com.google.gson.Gson;

public class MapDemo {
	
	
	/**
	 * 放东西
	 * 键值对
	 * 小宝马 xiaobaoma    key  K  黄楷睿 value V
	 * 小猴子  xiaohouzi    key   黄梓睿 value
	 * @throws Exception
	 */
	@Test
	public void add() throws Exception {
		
		List<String> list  = new ArrayList<String>();
		
		Map<String,String>  childMap = new HashMap<String,String>();
		
		childMap.put("xiaobaoma", "黄楷睿");
		childMap.put("xiaohouzi", "黄梓睿");
		
		System.out.println(childMap.size());
		
	}
	
	
	/**
	 * 怎么取东西? API  定义：  sun  根据规则去玩.....
	 * @throws Exception
	 */
	@Test
	public void get() throws Exception {
		
		//List<String> list  = new ArrayList<String>();
		
		Map<String,String>  childMap = new HashMap<String,String>();
		
		childMap.put("xiaobaoma", "黄楷睿");
		childMap.put("xiaohouzi", "黄梓睿");
		
		
		String name =childMap.get("xiaohouzi");
		System.out.println(name);
		
		//System.out.println(childMap.size());
		
	}
	
	
	
	/**
	 * 遍历
	 * 
	 * 映射： 绕道走   故事： 太阳照在小狗身上，出现狗影  太阳照在猪身上，出现猪影
	 * 
	 * 抽象：1只Object1个影子   1对1关系?
	 * 
	 * 
	 * 1对1的什么关系?  
	 * 		大名和小名的关系。 让小名确定   让大名变化。
	 * 		人的身份证号码跟姓名
	 * 
	 * 
	 * 映射： 实体类中的属性  映射   表中的字段
	 * 		 实体类             映射   表
	 * 		某1对象                 映射   id=2 这个表中的记录
	 * 
	 * 		
	 * 
	 * 
	 * 
	 * @throws Exception
	 */
	@Test
	public void it() throws Exception {
		Map<String,String>  childMap = new HashMap<String,String>();
		childMap.put("damao", "xxx");
		childMap.put("xiaomao", "yyy");
		Map<String,String>  idmap = new HashMap<String,String>();
		//遍历?  array list set = 遍历器 = 迭代器
	}
	
	
	/**
	 * 很多事情不是我们死记硬背记住的。而是我们去尝试，然后做实验得到...
	 * @throws Exception
	 */
	@Test
	public void studentTest() throws Exception {
		Map<String,String>  students = new HashMap<String,String>();
		
		
		// Alt不动  再按Tab可以快速切换
		students.put("10", "陈叶");
		students.put("11", "杨越");
		students.put("12", "小昭");
		students.put("13", "周芷若");
		students.put("14", "张无忌");
		
		
		String name = students.get("13");
		System.err.println(name);
		
		Set<String> sets = students.keySet();
		//System.out.println(sets);
		
		Iterator<String> it = sets.iterator();
		System.err.println("=====================================");
		while(it.hasNext()){
			
			//拿到里面的东东
			String key = it.next();
			
			// t = tab
			System.err.println("[key ="+key  + ",\tvalue=" + students.get(key) +"]");
			
		}
		//遍历?  array list set = 遍历器 = 迭代器
	}
	
	
	
	/**
	 * keySet 拿到的仅仅是key集合  没有value的集合
	 * 高级的for循环
	 * @throws Exception
	 */
	@Test
	public void forGaoJi() throws Exception {
		Map<String,String>  students = new HashMap<String,String>();
		
		
		// Alt不动  再按Tab可以快速切换
		students.put("10", "陈叶");
		students.put("11", "杨越");
		students.put("12", "小昭");
		students.put("13", "周芷若");
		students.put("14", "张无忌");
		
		Set<String>  sets = students.keySet();
		
		Gson  json  = new Gson();
		for(String key : sets){
			
			System.out.println(json.toJson(students.get(key)));;
		}
		
		
	}
	
	
	/**
	 * 不需要掌握
	 * @throws Exception
	 */
	@Test
	public void forGaoJi02() throws Exception {
		Map<String,String>  students = new HashMap<String,String>();
		
		
		//Alt 不动  再按Tab可以快速切换
		students.put("10", "陈叶");
		students.put("11", "杨越");
		students.put("12", "小昭");
		students.put("13", "周芷若");
		students.put("14", "张无忌");
		
		//entrySet 拿到的是key和value的集合
		Set<Entry<String, String>>  xx = students.entrySet();
		
		//new + alt +/  再写类名
		Gson json = new Gson();
		
		System.err.println(xx);
		for(Entry<String, String> yy : xx){
			//System.out.println( "[key="+yy.getKey() + ",value="+ yy.getValue() +"]");
			System.out.println(json.toJson(yy));
			
		}
		
		/*Gson  json  = new Gson();
		for(String key : sets){
			
			System.out.println(json.toJson(students.get(key)));;
		}*/
		
		
	}
	
	
	@Test
	public void delete() throws Exception {
		Map<String,String>  students = new HashMap<String,String>();
		
		
		// Alt不动  再按Tab可以快速切换
		students.put("10", "陈叶");
		students.put("11", "杨越");
		students.put("12", "小昭");
		students.put("13", "周芷若");
		students.put("14", "张无忌");
		
		
		String name = students.get("13");
		System.err.println(name);
		
		students.remove("13");
		
		Set<String> sets = students.keySet();
		//System.out.println(sets);
		
		Iterator<String> it = sets.iterator();
		System.err.println("=====================================");
		while(it.hasNext()){
			
			//拿到里面的东东
			String key = it.next();
			
			// t = tab
			System.err.println("[key ="+key  + ",\tvalue=" + students.get(key) +"]");
			
		}
		//遍历?  array list set = 遍历器 = 迭代器
	}
	
	
	@Test
	public void update() throws Exception {
		Map<String,String>  students = new HashMap<String,String>();
		
		
		// Alt不动  再按Tab可以快速切换
		students.put("10", "陈叶");
		students.put("11", "杨越");
		students.put("12", "小昭");
		students.put("13", "周芷若");
		students.put("14", "张无忌");
		
		
		String name = students.get("13");
		System.err.println(name);
		
		students.put("13", "赵敏");
		
		Set<String> sets = students.keySet();
		//System.out.println(sets);
		
		Iterator<String> it = sets.iterator();
		System.err.println("=====================================");
		while(it.hasNext()){
			
			//拿到里面的东东
			String key = it.next();
			
			// t = tab
			System.err.println("[key ="+key  + ",\tvalue=" + students.get(key) +"]");
			
		}
		//遍历?  array list set = 遍历器 = 迭代器
	}
	
	

}
